package com.chunxuan.langquan.support.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.LqProvinceBean;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.ui.adapter.AreaAdapter;
import com.chunxuan.langquan.ui.adapter.CityAdapter;
import com.chunxuan.langquan.ui.adapter.ProvinceAdapter;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class JDCityPicker extends PopupWindow {
    View view;
    Context mContext;
    RecyclerView mRvProvince;
    RecyclerView mRvCity;
    RecyclerView mRvArea;
    List<LqProvinceBean> lqCityList=new ArrayList<>();
    List<LqProvinceBean> lqAreaList=new ArrayList<>();
    ProvinceAdapter mProvinceAdapter;
    CityAdapter mCityAdapter;
    AreaAdapter mAreaAdapter;
    String province, city, area;
    int province_id, city_id, area_id;
    TextView mTvProvince;
    TextView mTvCity;
    TextView mTvArea;
    TextView cur_city_name_tv;
    ImageView mImgClose;
    LinearLayout mLlSelect;
    onCitySelect citySelect;
    List<LqProvinceBean> lqProvinceBeanList;
    String pro1,city1,area1;

    public JDCityPicker(Context context,List<LqProvinceBean> lqProvinceBeanList, onCitySelect citySelect,String pro,String city,String area) {
        super(context);
        this.mContext = context;
        this.lqProvinceBeanList=lqProvinceBeanList;
        this.citySelect = citySelect;
        this.pro1=pro;
        this.city1=city;
        this.area1=area;
        init(context);
    }

    private void init(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.dialog_city_picker, null);
        mRvProvince = view.findViewById(R.id.rv_province);
        mRvCity = view.findViewById(R.id.rv_city);
        mRvArea = view.findViewById(R.id.rv_area);
        mTvProvince = view.findViewById(R.id.tv_province);
        mTvCity = view.findViewById(R.id.tv_city);
        mTvArea = view.findViewById(R.id.tv_area);
        mLlSelect = view.findViewById(R.id.ll_select);
        mImgClose = view.findViewById(R.id.img_close);
        cur_city_name_tv=view.findViewById(R.id.cur_city_name_tv);

        if(!TextUtils.isEmpty(pro1)&&!TextUtils.isEmpty(city1)&&!TextUtils.isEmpty(area1)){
            cur_city_name_tv.setText(pro1+city1+area1);
        }else{
            cur_city_name_tv.setText("中国大陆");
        }

        this.setContentView(view);
        this.setBackgroundDrawable(new BitmapDrawable());

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int height = (int) (wm.getDefaultDisplay().getHeight() * 0.8);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(height);
        this.setFocusable(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            this.setClippingEnabled(false);
            this.setOutsideTouchable(false);
        }
        this.setAnimationStyle(R.style.PopupWindow);

        initProvince();

        mImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mTvProvince.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                province = "";
                city = "";
                mTvCity.setVisibility(View.GONE);
                mTvProvince.setVisibility(View.GONE);
                mLlSelect.setVisibility(View.VISIBLE);

                mRvProvince.setVisibility(View.VISIBLE);
                mRvCity.setVisibility(View.GONE);
                mRvArea.setVisibility(View.GONE);
            }
        });

        mTvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                city = "";
                mTvCity.setVisibility(View.GONE);
//                initCity();
                mRvProvince.setVisibility(View.GONE);
                mRvCity.setVisibility(View.VISIBLE);
                mRvArea.setVisibility(View.GONE);
            }
        });

       /* mTvArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initArea();
            }
        });*/
    }


    private void initProvince() {
        mRvProvince.setVisibility(View.VISIBLE);
        mRvCity.setVisibility(View.GONE);
        mRvArea.setVisibility(View.GONE);
        mProvinceAdapter = new ProvinceAdapter(lqProvinceBeanList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvProvince.setLayoutManager(linearLayoutManager);
        mRvProvince.setAdapter(mProvinceAdapter);
        mProvinceAdapter.setOnItemListener(new ProvinceAdapter.onItemClick() {
            @Override
            public void onClick(int position, int provinceId) {
                mProvinceAdapter.setSelection(position);
                mProvinceAdapter.notifyDataSetChanged();
                String name=lqProvinceBeanList.get(position).getName();
                if (TextUtils.isEmpty(province)) {
                    province = name;
                    province_id=lqProvinceBeanList.get(position).getId();
                    mTvProvince.setVisibility(View.VISIBLE);
                    mTvProvince.setText(name);
                    getCityByProvId(lqProvinceBeanList.get(position).getId());
                }
            }
        });
        mProvinceAdapter.notifyDataSetChanged();
    }
    private Disposable getCityByProvId(int prov_id) {
        return APIRetrofit.getDefault().getCityByProvId(Global.HEADER,prov_id)
                .compose(RxUtil.<BaseResult2<List<LqProvinceBean>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<LqProvinceBean>>>() {
                    @Override
                    public void accept(BaseResult2<List<LqProvinceBean>> result2) throws Exception {
//                        hideProgress();
                        if(result2.isSuccess()){
                            lqCityList=result2.getData();
                            initCity();
                        }else{
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取城市列表失败");
                    }
                });
    }
    private void initCity() {
        mRvProvince.setVisibility(View.GONE);
        mRvCity.setVisibility(View.VISIBLE);
        mRvArea.setVisibility(View.GONE);
        mCityAdapter = new CityAdapter(lqCityList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvCity.setLayoutManager(linearLayoutManager);
        mRvCity.setAdapter(mCityAdapter);
        mCityAdapter.setOnItemListener(new CityAdapter.onItemClick() {
            @Override
            public void onClick(int position, int cityId) {
                String name=lqCityList.get(position).getName();
                if (TextUtils.isEmpty(city)) {
                    city = name;
                    city_id=lqCityList.get(position).getId();
                    mTvCity.setVisibility(View.VISIBLE);
                    mTvCity.setText(city);
                    getAreaByCityId(lqCityList.get(position).getId());
                }
            }
        });
    }
    private Disposable getAreaByCityId(int city_id) {
        return APIRetrofit.getDefault().getAreaByCityId(Global.HEADER,city_id)
                .compose(RxUtil.<BaseResult2<List<LqProvinceBean>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<LqProvinceBean>>>() {
                    @Override
                    public void accept(BaseResult2<List<LqProvinceBean>> result2) throws Exception {
//                        hideProgress();
                        if(result2.isSuccess()){
                            lqAreaList=result2.getData();
                            initArea();
                        }else{
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取市区列表失败");
                    }
                });
    }

    private void initArea() {
        mRvProvince.setVisibility(View.GONE);
        mRvCity.setVisibility(View.GONE);
        mRvArea.setVisibility(View.VISIBLE);
        mAreaAdapter = new AreaAdapter(lqAreaList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvArea.setLayoutManager(linearLayoutManager);
        mRvArea.setAdapter(mAreaAdapter);
        mAreaAdapter.setOnItemListener(new AreaAdapter.onItemClick() {
            @Override
            public void onClick(int position, String name) {
                if (TextUtils.isEmpty(area)) {
                    area = name;
                    area_id=lqAreaList.get(position).getId();
                    mTvArea.setVisibility(View.VISIBLE);
                    mTvArea.setText(area);
                    mLlSelect.setVisibility(View.GONE);
                    citySelect.onSelect(province, city, area,province_id,city_id,area_id);
                    dismiss();
                }
            }
        });
    }

    public interface onCitySelect {
        void onSelect(String province, String city, String area,int province_id,int city_id,int area_id);
    }
}
